﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using Model;

public partial class MembershipCard_wechat_web_SubscribeDefault : System.Web.UI.Page
{
    StoreNameBO snbo = new StoreNameBO();
    StaffBO sbo = new StaffBO();
    CardUserBO cbo = new CardUserBO();
    SubscribeBO subo = new SubscribeBO();
    ProjectBO pbo = new ProjectBO();
    ProjectForStoreBO fbo = new ProjectForStoreBO();
    public string FromUserName = "";
    public string ToUserName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
            {
                string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                Session["FromUserName"] = user;
                Session["ToUserName"] = user2;
                FromUserName = user;
                ToUserName = user2;
                for (int i = 0; i <= 30; i++)
                {
                    ddlDate.Items.Add(new ListItem(DateTime.Now.AddDays(i).ToString("yyyy-MM-dd") + " " + GetWeek(DateTime.Now.AddDays(i)), DateTime.Now.AddDays(i).ToString("yyyy-MM-dd")));
                }
                DataTable dtStore = snbo.GetStoreByToUserName(user2);
                foreach (DataRow dr in dtStore.Rows)
                {
                    ddlStore.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                }
                DataTable dtProject = pbo.GetProjectByToUserName(user2);
                foreach (DataRow dr in dtProject.Rows)
                {
                    ddlProject.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                }
                ddlStore_SelectedIndexChanged(null, null);
                CardUser c = cbo.GetModel(user);
                if (c == null)
                {
                    Label1.Text = "你不是我们的会员，点击下面的按钮，成为我们的会员";
                    Button1.Visible = false;
                    wx.Visible = true;
                    tb.Visible = false;
                }
                else
                {
                    lblName.Text = c.Name;
                    lblPhone.Text = c.Phone;
                }
                if (Request.QueryString["store"] != null&&Request.QueryString["sex"]!=null)
                {
                    ddlStore.SelectedValue = Request.QueryString["store"].ToString();
                    ddlPerSex.SelectedValue = Request.QueryString["sex"].ToString();
                    ddlStore_SelectedIndexChanged(null, null);
                }
            }
        }
    }

    public string GetWeek(DateTime d)
    {
        string[] Day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
        string week = Day[Convert.ToInt32(d.DayOfWeek.ToString("d"))].ToString();
        return week;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
        {
            try
            {
           
            string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
            string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
            CardUser c = cbo.GetModel(user);
            if (c != null)
            {
                Subscribe s = new Subscribe();
                s.CID = c.ID;
                s.PerCount = int.Parse(ddlPerCount.SelectedValue);
                s.SID = int.Parse(ddlPerName.SelectedValue);
                s.Time = DateTime.Parse(ddlDate.SelectedValue+" "+ddlTime.SelectedValue+":"+ddlMin.SelectedValue+":00");
                s.SNID = int.Parse(ddlStore.SelectedValue);
                s.SexID = int.Parse(ddlPerSex.SelectedValue);
                s.States = 0;
                s.PID = int.Parse(ddlProject.SelectedValue);
               int num=  subo.Add(s);
               if (num > 0)
               {
                   Label1.Text = "提交成功，工作人员会第一时间处理";
                   Button1.Enabled = false;
                   string dx1 = string.Format("预约人：{0} 手机：{1} 时间：{2}  人数：{3}  技师性别要求：{4}  指定技师：{5}  项目：{7}  店名：{6}",lblName.Text,lblPhone.Text,DateTime.Parse(s.Time.ToString()).ToString("yyyy-MM-dd HH:mm:ss"),s.PerCount,ddlPerSex.SelectedItem.Text,ddlPerName.SelectedItem.Text,ddlStore.SelectedItem.Text,ddlProject.SelectedItem.Text);
                   string dx2 = string.Format("感谢您预约{0}，您预约的时间是{1}，预约成功，预约保留15分钟，如有变故请联系本店。", ddlStore.SelectedItem.Text, DateTime.Parse(s.Time.ToString()).ToString("yyyy-MM-dd HH:mm:ss"));
                   try
                   {
                       StoreName f = snbo.GetModel(int.Parse(s.SNID.ToString()));
                       if (f != null)
                       {
                           new SendDX().Send(dx1, f.Phone,user2);
                           //new SendDX().Send(dx2, lblPhone.Text);
                       }
                   }
                   catch (Exception)
                   {
                       
                   }
               }
            }
            }
            catch (Exception ex)
            {
                Label1.Text = ex.Message.ToString();
            }
        }

    }

    protected void ddlStore_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlStore.Items.Count > 0)
        {
            ddlPerName.Items.Clear();
            string where = "";
            if (ddlPerSex.SelectedValue != "0")
                where = " and sex=" + ddlPerSex.SelectedValue;
            List<Staff> list = sbo.GetModelList("  sid=" + ddlStore.SelectedValue + where);
            ddlPerName.Items.Add(new ListItem("随机","0"));
            foreach (Staff s in list)
            {
                ddlPerName.Items.Add(new ListItem(s.Name, s.ID.ToString()));
            }

            ddlProject.Items.Clear();
            List<ProjectForStore> list2 = fbo.GetModelList("sid=" + ddlStore.SelectedValue);
            ddlProject.Items.Add(new ListItem("项目选择", "0"));
            foreach (ProjectForStore p in list2)
            {
                ddlProject.Items.Add(new ListItem(p.Name, p.PID.ToString()));
            }
        }
    }
}